#!/bin/bash

DOCKER_REGISTRY=${DOCKER_REGISTRY:-'127.0.0.1:5042'}

sudo mkdir -p /opt/solum
sudo chown -R ${USER}:${USER} /opt/solum

sudo mkdir -p /var/log/solum
sudo chown -R ${USER}:${USER} /var/log/solum

mkdir -p /opt/solum/apps
mkdir -p /opt/solum/cache

# Slugbuilder builds the cedarish slug
# We should own/customize this for solum
# but for POC we can grab and build container
echo Creating slugbuilder container
docker pull solum/slugbuilder
docker tag solum/slugbuilder $DOCKER_REGISTRY/slugbuilder

# Slugtester runs unit tests against user code
echo Creating slugtester container
docker pull solum/slugtester
docker tag solum/slugtester $DOCKER_REGISTRY/slugtester

# Slugrunner runs the cedarish slug built
# by slugbuilder.
# We should own/customize this for solum
# now available from trusted builds.
echo Creating slugrunner container
docker pull solum/slugrunner
docker tag solum/slugrunner $DOCKER_REGISTRY/slugrunner

if [ ! -d /opt/solum/buildpacks ]; then
    echo Preparing Buildpacks
    mkdir -p /opt/solum/buildpacks
    pushd /opt/solum/buildpacks
    # These buildpacks help with autodetection and app level depedency installs.
    git clone --depth 1 https://github.com/heroku/heroku-buildpack-java.git
    git clone --depth 1 https://github.com/heroku/heroku-buildpack-ruby.git
    git clone --depth 1 https://github.com/heroku/heroku-buildpack-python.git
    git clone --depth 1 https://github.com/gabrtv/heroku-buildpack-nodejs
    git clone --depth 1 https://github.com/heroku/heroku-buildpack-play.git
    git clone --depth 1 https://github.com/CHH/heroku-buildpack-php.git
    git clone --depth 1 https://github.com/heroku/heroku-buildpack-clojure.git
    git clone --depth 1 https://github.com/kr/heroku-buildpack-go.git
    git clone --depth 1 https://github.com/heroku/heroku-buildpack-scala
    git clone --depth 1 https://github.com/igrigorik/heroku-buildpack-dart.git
    git clone --depth 1 https://github.com/miyagawa/heroku-buildpack-perl.git
    git clone --depth 1 https://github.com/paulczar/heroku-buildpack-mono.git
    popd
fi
